﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using MayhemCore;
using MayhemWpf.ModuleTypes;
using System.Net.NetworkInformation;
using MayhemWpf.UserControls;
using ComputerMayhem.Wpf;
using Microsoft.WindowsAPICodePack.Net;
using System.Threading;

namespace ComputerMayhem.Events
{

    [Serializable]
    public class NetworkNameOptions
    {
        public Guid Id { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }

        public override bool Equals(object obj)
        {
            NetworkNameOptions other = obj as NetworkNameOptions;
            if (other != null)
            {
                return this.Id == other.Id && this.Name == other.Name;
            }
            return false;
        }
    }

    [DataContract]
    [MayhemModule("Network Name", "Triggers when connecting to a particular network name")]
    public class NetworkName : EventBase, IWpfConfigurable
    {
        private bool triggered = false;

        [DataMember]
        private NetworkNameOptions options;

        protected override void OnEnabling(EnablingEventArgs e)
        {
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
        }

        void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            if (NetworkListManager.IsConnected)
            {
                Thread.Sleep(10 * 1000);


                if (DateTime.Now - NetworkListManager.GetNetwork(options.Id).ConnectedTime > TimeSpan.FromMinutes(1))
                {
                    ResetTrigger();
                }

                var connectedNetworks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.All);

                foreach (var item in connectedNetworks)
                {
                    var timeConnected = DateTime.Now - item.ConnectedTime;
                    bool correctNetwork = item.IsConnected && item.NetworkId.Equals(options.Id);

                    if (correctNetwork && timeConnected < TimeSpan.FromMinutes(1) && !triggered)
                    {
                        Trigger();
                        triggered = true;
                        return;

                    }
                }
                
            }
        }

        protected override void OnDisabled(DisabledEventArgs e)
        {
            NetworkChange.NetworkAddressChanged -= new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

            ResetTrigger();
        }

        private void ResetTrigger()
        {
            triggered = false;
        }

        public WpfConfiguration ConfigurationControl
        {
            get
            {
                return new NetworkNameConfig(options);
            }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            NetworkNameConfig config = configurationControl as NetworkNameConfig;
            options = config.Options;

        }

        public string GetConfigString()
        {
            return "When connecting to " + options.Name;
        }
    }
}
